#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "clockconstant.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    isPause = false;
    isWorkTime = false;
    initTimer();
    initMessageBox();

    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete timer;
    delete mb;
    delete ui;
}

void MainWindow::initTimer()
{
    timer = new QTimer();
    timer->setInterval(1000);
    connect(timer,SIGNAL(timeout()),this,SLOT(onTimeOut()));
}

void MainWindow::initMessageBox()
{
    mb = new QMessageBox("提示", "", QMessageBox::Question, QMessageBox::Ok | QMessageBox::Default, NULL, 0);
    mb->setWindowFlags(Qt::WindowStaysOnTopHint);
}

void MainWindow::on_btn_start_clicked()
{
    ui->time_label->setText(workTimeStr);
    ui->time_property_label->setText("工作时间");
    isWorkTime = true;
    timer->start();
}

void MainWindow::onTimeOut()
{
    if (ui->time_label->text() == "00:00") {
        isWorkTime = !isWorkTime;
        QString info, resetTime, timeProperty;

        if (isWorkTime) {
            info = "工作时间到";
            resetTime = workTimeStr;
            timeProperty = "工作时间";
        } else {
            info = "休息时间到";
            resetTime = restTimeStr;
            timeProperty = "休息时间";
        }

        ui->time_label->setText(resetTime);
        ui->time_property_label->setText(timeProperty);
        mb->setText(info);
        mb->show();
    }
    QDateTime dateTime = QDateTime::fromString(ui->time_label->text(), "mm:ss");
    ui->time_label->setText(dateTime.addSecs(-1).toString("mm:ss"));
}

void MainWindow::on_btn_clear_clicked()
{
    timer->stop();
    ui->time_label->setText("00:00");
    ui->time_property_label->setText("");
}

void MainWindow::on_btn_pause_clicked()
{
    isPause = !isPause;
    if (isPause) {
        timer->stop();
    } else {
        timer->start();
    }
}
